﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using cwtrock.view;
using cwtrock.model;
using cwtrock.syscommon;
using cwtrock.service.impl;
using System.Text.RegularExpressions;
using cwtrock.view.handle;
namespace cwtrock.view
{
    partial class AccountsMainFrm : Form
    {
        private AccountsServiceImpl accountsServiceImpl = new AccountsServiceImpl();
        // 15
        private string space = "               ";

        private string selacctcode = "";
  
        private string selacctname = "";

        // 选中的树结点是作为选择条件的开始或结束的标记 0，开始 1，结束
        private string selflag = "0";

        private TreeNode treeNodeSelected;


     //   private delegate TreeNode TreeSelectNodeDelegate();
     //   private delegate void TreeRefreshTreeViewDelegate();


        #region 单体模式
        //单体模式
        private static AccountsMainFrm frm = null;
        /// <summary>
        /// 单体模式
        /// </summary>
        /// <returns></returns>
        public static AccountsMainFrm getInstance(string oktext)
        {
            if (frm == null)
            {
                frm = new AccountsMainFrm();
                frm.okbtn.Text = oktext;
            }

            return frm;
        }
        #endregion

        public AccountsMainFrm()
        {
            this.TopMost = true;
            InitializeComponent();
            initFortuneTree();
            initLiabilityTree();
            initCommonTree();
            initShareTree();
            initCostTree();
            initIncomeTree();
        
        }

        // 初始化资金树
        private void initFortuneTree()
        {
            fortuneTree.ImageList = imageList1;
            List<Accounts> actls = accountsServiceImpl.getAcctsByCatalog(SysConst.ACCT_CODE_FORTUNE);

            foreach(Accounts acct in actls){
                if ("0".Equals(acct.Parentid)) {

                    TreeNode node = new TreeNode();
                     node.Text = acct.Acctcode + space + acct.Acctname;
                     node.Name = acct.Acctcode;
                     node.ImageIndex = 1;
                     node.SelectedImageIndex = 1;
                     fortuneTree.Nodes.Add(node);
                    buildFortuneTree(node,acct.Acctcode,actls);

                }

            }

          //  fortuneTree.ex;

        }


        // 初始化负债树
        private void initLiabilityTree()
        {
            liabilitytree.ImageList = imageList1;
            List<Accounts> actls = accountsServiceImpl.getAcctsByCatalog(SysConst.ACCT_CODE_LIABILITY);

            foreach (Accounts acct in actls)
            {
                if ("0".Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;
                    liabilitytree.Nodes.Add(node);
                    buildLiabilityTree(node, acct.Acctcode, actls);

                }

            }

          //  liabilitytree.ExpandAll();

        }


        // 初始化共同树
        private void initCommonTree()
        {
            commontree.ImageList = imageList1;
            List<Accounts> actls = accountsServiceImpl.getAcctsByCatalog(SysConst.ACCT_CODE_COMMON);

            foreach (Accounts acct in actls)
            {
                if ("0".Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;
                    commontree.Nodes.Add(node);
                    buildCommonTree(node, acct.Acctcode, actls);

                }

            }

        //    commontree.ExpandAll();

        }

        // 初始化权益树
        private void initShareTree()
        {
            sharetree.ImageList = imageList1;
            List<Accounts> actls = accountsServiceImpl.getAcctsByCatalog(SysConst.ACCT_CODE_SHARE);

            foreach (Accounts acct in actls)
            {
                if ("0".Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;
                    sharetree.Nodes.Add(node);
                    buildShareTree(node, acct.Acctcode, actls);

                }

            }

      //      sharetree.ExpandAll();

        }

        // 初始化成本树
        private void initCostTree()
        {
            costtree.ImageList = imageList1;
            List<Accounts> actls = accountsServiceImpl.getAcctsByCatalog(SysConst.ACCT_CODE_COST);

            foreach (Accounts acct in actls)
            {
                if ("0".Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;
                    costtree.Nodes.Add(node);
                    buildCostTree(node, acct.Acctcode, actls);

                }

            }

       //     costtree.ExpandAll();

        }

        // 初始化损益树
        private void initIncomeTree()
        {
            incometree.ImageList = imageList1;
            List<Accounts> actls = accountsServiceImpl.getAcctsByCatalog(SysConst.ACCT_CODE_INCOME);

            foreach (Accounts acct in actls)
            {
                if ("0".Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;
                    incometree.Nodes.Add(node);
                    buildIncomeTree(node, acct.Acctcode, actls);

                }

            }

  //          incometree.ExpandAll();

        }
   
        // 递归构造资产树
        private void buildFortuneTree(TreeNode parentnode, string parentid, List<Accounts> actls)
        {
           
            foreach (Accounts acct in actls)
            {
                if (parentid.Equals(acct.Parentid))
                {
                    
                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                     node.Name = acct.Acctcode;
                     parentnode.Nodes.Add(node);

                     parentnode.ImageIndex = 0;
                     parentnode.SelectedImageIndex = 0;
                     node.ImageIndex = 1;
                     node.SelectedImageIndex = 1;

                     buildFortuneTree(node, acct.Acctcode, actls);

                }

            }
        
        
        }


        // 递归构造负债树
        private void buildLiabilityTree(TreeNode parentnode, string parentid, List<Accounts> actls)
        {

            foreach (Accounts acct in actls)
            {
                if (parentid.Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    parentnode.Nodes.Add(node);

                    parentnode.ImageIndex = 0;
                    parentnode.SelectedImageIndex = 0;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;

                    buildLiabilityTree(node, acct.Acctcode, actls);

                }

            }


        }


        // 递归构造共同树
        private void buildCommonTree(TreeNode parentnode, string parentid, List<Accounts> actls)
        {

            foreach (Accounts acct in actls)
            {
                if (parentid.Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    parentnode.Nodes.Add(node);

                    parentnode.ImageIndex = 0;
                    parentnode.SelectedImageIndex = 0;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;

                    buildCommonTree(node, acct.Acctcode, actls);

                }

            }


        }

        // 递归构造权益树
        private void buildShareTree(TreeNode parentnode, string parentid, List<Accounts> actls)
        {

            foreach (Accounts acct in actls)
            {
                if (parentid.Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    parentnode.Nodes.Add(node);

                    parentnode.ImageIndex = 0;
                    parentnode.SelectedImageIndex = 0;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;

                    buildShareTree(node, acct.Acctcode, actls);

                }

            }


        }


        // 递归构造成本树
        private void buildCostTree(TreeNode parentnode, string parentid, List<Accounts> actls)
        {

            foreach (Accounts acct in actls)
            {
                if (parentid.Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    parentnode.Nodes.Add(node);

                    parentnode.ImageIndex = 0;
                    parentnode.SelectedImageIndex = 0;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;

                    buildCostTree(node, acct.Acctcode, actls);

                }

            }


        }


        // 递归构造损益树
        private void buildIncomeTree(TreeNode parentnode, string parentid, List<Accounts> actls)
        {

            foreach (Accounts acct in actls)
            {
                if (parentid.Equals(acct.Parentid))
                {

                    TreeNode node = new TreeNode();
                    node.Text = acct.Acctcode + space + acct.Acctname;
                    node.Name = acct.Acctcode;
                    parentnode.Nodes.Add(node);

                    parentnode.ImageIndex = 0;
                    parentnode.SelectedImageIndex = 0;
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;

                    buildIncomeTree(node, acct.Acctcode, actls);

                }

            }


        }


        // 添加树结点
        private void addaccount_Click(object sender, EventArgs e)
        {
           
            if (accounttab.SelectedIndex == 0)
            {
                addFortuneNode();           
            }

            else  if (accounttab.SelectedIndex == 1)
            {
                addLiabilityNode();
            }

            else if (accounttab.SelectedIndex == 2)
            {
                addCommonNode();
            }
            else if (accounttab.SelectedIndex == 3)
            {
                addShareNode();
            }
            else if (accounttab.SelectedIndex == 4)
            {
                addCostNode();
            }
            else if (accounttab.SelectedIndex == 5)
            {
                addIncomeNode();
            }

        }


        // 添加资产结点
        private void addFortuneNode()
        {

            string parentid = "0";
            if (fortuneTree.SelectedNode != null)
            {
                parentid = fortuneTree.SelectedNode.Name;
            }
            Accounts account = new Accounts();
            account.Acctcatalog = SysConst.ACCT_CODE_FORTUNE;

            Addaccount addfrm = new Addaccount(parentid, account);
            DialogResult rlt = addfrm.ShowDialog();

            if (rlt == DialogResult.OK)
            {
                account = addfrm.CurAccount;
                TreeNode node = new TreeNode();
                node.Text = account.Acctcode + space + account.Acctname;
                node.Name = account.Acctcode;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;

                if (fortuneTree.SelectedNode == null)
                {
                    fortuneTree.Nodes.Add(node);
                }

                else
                {
                    fortuneTree.SelectedNode.Nodes.Add(node);
                    fortuneTree.SelectedNode.ImageIndex = 0;
                    fortuneTree.SelectedNode.SelectedImageIndex = 0;
                }

     //           fortuneTree.ExpandAll();

            }

        }

        // 添加负债结点
        private void addLiabilityNode() {

            string parentid = "0";
            if (liabilitytree.SelectedNode != null)
            {
                parentid = liabilitytree.SelectedNode.Name;
            }
            Accounts account = new Accounts();
            account.Acctcatalog = SysConst.ACCT_CODE_LIABILITY;

            Addaccount addfrm = new Addaccount(parentid, account);
            DialogResult rlt = addfrm.ShowDialog();

            if (rlt == DialogResult.OK)
            {
                account = addfrm.CurAccount;
                TreeNode node = new TreeNode();
                node.Text = account.Acctcode + space + account.Acctname;
                node.Name = account.Acctcode;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;

                if (liabilitytree.SelectedNode == null)
                {
                    liabilitytree.Nodes.Add(node);
                }

                else
                {
                    liabilitytree.SelectedNode.Nodes.Add(node);
                    liabilitytree.SelectedNode.ImageIndex = 0;
                    liabilitytree.SelectedNode.SelectedImageIndex = 0;
                }

      //          liabilitytree.ExpandAll();

            }
        
        }

        // 添加共同结点
        private void addCommonNode()
        {

            string parentid = "0";
            if (commontree.SelectedNode != null)
            {
                parentid = commontree.SelectedNode.Name;
            }
            Accounts account = new Accounts();
            account.Acctcatalog = SysConst.ACCT_CODE_COMMON;

            Addaccount addfrm = new Addaccount(parentid,account);
            DialogResult rlt = addfrm.ShowDialog();

            if (rlt == DialogResult.OK)
            {
                account = addfrm.CurAccount;
                TreeNode node = new TreeNode();
                node.Text = account.Acctcode + space + account.Acctname;
                node.Name = account.Acctcode;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;

                if (commontree.SelectedNode == null)
                {
                    commontree.Nodes.Add(node);
                }

                else
                {
                    commontree.SelectedNode.Nodes.Add(node);
                    commontree.SelectedNode.ImageIndex = 0;
                    commontree.SelectedNode.SelectedImageIndex = 0;
                }

      //          commontree.ExpandAll();

            }

        }


        // 添加权益结点
        private void addShareNode()
        {

            string parentid = "0";
            if (sharetree.SelectedNode != null)
            {
                parentid = sharetree.SelectedNode.Name;
            }
            Accounts account = new Accounts();
            account.Acctcatalog = SysConst.ACCT_CODE_SHARE;

            Addaccount addfrm = new Addaccount(parentid,account);
            DialogResult rlt = addfrm.ShowDialog();

            if (rlt == DialogResult.OK)
            {
                account = addfrm.CurAccount;
                TreeNode node = new TreeNode();
                node.Text = account.Acctcode + space + account.Acctname;
                node.Name = account.Acctcode;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;

                if (sharetree.SelectedNode == null)
                {
                    sharetree.Nodes.Add(node);
                }

                else
                {
                    sharetree.SelectedNode.Nodes.Add(node);
                    sharetree.SelectedNode.ImageIndex = 0;
                    sharetree.SelectedNode.SelectedImageIndex = 0;
                }

      //          sharetree.ExpandAll();

            }

        }


        // 添加成本结点
        private void addCostNode()
        {

            string parentid = "0";
            if (costtree.SelectedNode != null)
            {
                parentid = costtree.SelectedNode.Name;
            }
            Accounts account = new Accounts();
            account.Acctcatalog = SysConst.ACCT_CODE_COST;

            Addaccount addfrm = new Addaccount(parentid,account);
            DialogResult rlt = addfrm.ShowDialog();

            if (rlt == DialogResult.OK)
            {
                account = addfrm.CurAccount;
                TreeNode node = new TreeNode();
                node.Text = account.Acctcode + space + account.Acctname;
                node.Name = account.Acctcode;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;

                if (costtree.SelectedNode == null)
                {
                    costtree.Nodes.Add(node);
                }

                else
                {
                    costtree.SelectedNode.Nodes.Add(node);
                    costtree.SelectedNode.ImageIndex = 0;
                    costtree.SelectedNode.SelectedImageIndex = 0;
                }

     //           costtree.ExpandAll();

            }

        }

        // 添加损益结点
        private void addIncomeNode()
        {

            string parentid = "0";
            if (incometree.SelectedNode != null)
            {
                parentid = incometree.SelectedNode.Name;
            }
            Accounts account = new Accounts();
            account.Acctcatalog = SysConst.ACCT_CODE_INCOME;

            Addaccount addfrm = new Addaccount(parentid,account);
            DialogResult rlt = addfrm.ShowDialog();

            if (rlt == DialogResult.OK)
            {
                account = addfrm.CurAccount;
                TreeNode node = new TreeNode();
                node.Text = account.Acctcode + space + account.Acctname;
                node.Name = account.Acctcode;
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;

                if (incometree.SelectedNode == null)
                {
                    incometree.Nodes.Add(node);
                }

                else
                {
                    incometree.SelectedNode.Nodes.Add(node);
                    incometree.SelectedNode.ImageIndex = 0;
                    incometree.SelectedNode.SelectedImageIndex = 0;
                }

      //          incometree.ExpandAll();

            }

        }

        private void AccountsMainFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm = null;
        }

        private void okbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public string Selacctname
        {
            get { return selacctname; }
            set { selacctname = value; }
        }

        public string Selacctcode
        {
            get { return selacctcode; }
            set { selacctcode = value; }
        }

        private void fortuneTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            treeNodeSelected = e.Node;
            selacctcode = e.Node.Name;
            string selacct = e.Node.Text;
            string[] selacctArr = Regex.Split(selacct, selacctcode, RegexOptions.IgnoreCase);
            
            if (selacctArr.Length>0)
              selacctname = selacctArr[1].Trim();
        }

        #region  编辑科目结点
        private void editaccount_Click(object sender, EventArgs e)
        {
            TreeNode selNode;
            TreeView treeView=null ;
            String catLog = "";

            if (accounttab.SelectedIndex == 0)
            {
                treeView = fortuneTree ;
                catLog = SysConst.ACCT_CODE_FORTUNE;
            }
            else if (accounttab.SelectedIndex == 1)
            {
               
                treeView = liabilitytree;
                catLog = SysConst.ACCT_CODE_LIABILITY;
            }
            else if (accounttab.SelectedIndex == 2)
            {
                treeView = commontree;
                catLog = SysConst.ACCT_CODE_COMMON; 
            }
            else if (accounttab.SelectedIndex == 3)
            {
                treeView = sharetree;
                catLog = SysConst.ACCT_CODE_SHARE;
            }
            else if (accounttab.SelectedIndex == 4)
            {
                treeView = costtree;
                catLog = SysConst.ACCT_CODE_COST; 
            }
            else if (accounttab.SelectedIndex == 5)
            {
                treeView = incometree;
                catLog = SysConst.ACCT_CODE_INCOME;
            }

             selNode = treeView.SelectedNode;

            if (selNode == null)
            {
                MessageBox.Show("请选择节点", "系统提示", MessageBoxButtons.OK);
                return;
            }

            string parentid = "0";
            if (costtree.SelectedNode != null)
            {
                parentid = treeView.SelectedNode.Name;
            }
            Accounts account = new Accounts();
            account.Acctcatalog = catLog;
            account.Acctcode = selNode.Name ;


            Addaccount addfrm = new Addaccount(parentid, account);
            addfrm.Visible = false;
            DialogResult rlt = addfrm.ShowDialog();

            if (rlt == DialogResult.OK)
            {
                account = addfrm.CurAccount;
                selNode.Text = account.Acctcode + space + account.Acctname;
                selNode.Name = account.Acctcode;
                selNode.ImageIndex = 1;
                selNode.SelectedImageIndex = 1;
                treeView.Refresh();
            }


        }
        #endregion 

        private void delaccount_Click(object sender, EventArgs e)
        {
            TreeNode selNode;
            TreeView treeView = null;

            if (accounttab.SelectedIndex == 0)
            {
                treeView = fortuneTree;
            }
            else if (accounttab.SelectedIndex == 1)
            {
                treeView = liabilitytree;
            }
            else if (accounttab.SelectedIndex == 2)
            {
                treeView = commontree;
            }
            else if (accounttab.SelectedIndex == 3)
            {
                treeView = sharetree;
            }
            else if (accounttab.SelectedIndex == 4)
            {
                treeView = costtree;
            }
            else if (accounttab.SelectedIndex == 5)
            {
                treeView = incometree;
            }

            selNode = treeView.SelectedNode;

            if (selNode == null)
            {
                MessageBox.Show("请选择节点", "系统提示", MessageBoxButtons.OK);
                return;
            }

            int rows = accountsServiceImpl.del(selNode.Name);
            if (rows == 1)
            {
                MessageBox.Show("删除成功！", "系统提示", MessageBoxButtons.OK);
                treeView.SelectedNode.Remove();
            }
            else
            {
                MessageBox.Show("删除失败！", "系统提示", MessageBoxButtons.OK);
            }
        }

        private void fortuneTree_DoubleClick(object sender, EventArgs e)
        {
            GeneralRptGuide grgfrm = GeneralRptGuide.getInstance();
            if (treeNodeSelected.FirstNode == null)
            {
                // 开始条件
                if ("0".Equals(selflag))
                {
                    grgfrm.writeAcctQueryFrom(selacctcode);
                }
                else
                {
                    grgfrm.writeAcctQueryEnd(selacctcode);
                }
                this.Close();
            }
        }


        public string Selflag
        {
            get { return selflag; }
            set { selflag = value; }
        }
    }
}
